{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Docker 部署 Mysql\n",
    "\n",
    "#### 系统环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "root@hecs-271888:~# cat  /etc/os-release\n",
    "PRETTY_NAME=\"Ubuntu 22.04.2 LTS\"\n",
    "NAME=\"Ubuntu\"\n",
    "VERSION_ID=\"22.04\"\n",
    "VERSION=\"22.04.2 LTS (Jammy Jellyfish)\"\n",
    "VERSION_CODENAME=jammy\n",
    "ID=ubuntu\n",
    "ID_LIKE=debian\n",
    "HOME_URL=\"https://www.ubuntu.com/\"\n",
    "SUPPORT_URL=\"https://help.ubuntu.com/\"\n",
    "BUG_REPORT_URL=\"https://bugs.launchpad.net/ubuntu/\"\n",
    "PRIVACY_POLICY_URL=\"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n",
    "UBUNTU_CODENAME=jammy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "source": [
    "获取MySQL容器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "docker pull mysql:latest"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行容器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=yourpassword -p 3306:3306 -v /my/own/datadir:/var/lib/mysql -d mysql:latest"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "执行成功后会给出一个ID `0bc0d88e5b3e3e9e14a96c07169ed30126994b3b519f2f47ae035036a51155cf`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "source": [
    "参数解析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--name my-mysql：设置容器名称。\n",
    "-e MYSQL_ROOT_PASSWORD=yourpassword：设置 root 用户密码。\n",
    "-p 3306:3306：将 MySQL 的 3306 端口映射到主机的 3306 端口。\n",
    "-v /my/own/datadir:/var/lib/mysql：将 MySQL 数据存储在主机目录 /my/own/datadir 中，实现数据持久化。\n",
    "-d：后台运行容器。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**验证连接** 使用 MySQL 客户端或工具连接到 `localhost:3306`，用户名为 `root`，密码为你设置的密码。\n",
    "\n",
    "安装MySQL客户端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "apt install mysql-client-core-8.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "验证客户端安装成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "root@hecs-271888:~# mysql --version\n",
    "# mysql  Ver 8.0.40-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "连接 MySQL 服务器：\n",
    "使用 MySQL 客户端连接到 MySQL 服务器："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "# 这里不需要加上密码，密码是后面增加的。\n",
    "mysql -h 127.0.0.1 -P 3306 -u root -p "
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
